/**
 * @author Mahesh
 */

var microBreakSettings = new Break("micro", null, null, null);
microBreakSettings.loadValues();
var macroBreakSettings = new Break("macro", null, null, null);
macroBreakSettings.loadValues();

var currentBreakSettings = microBreakSettings;
	
window.addEventListener('load', settingsOnloadFunction, false);
function settingsOnloadFunction()
{
	pickStoredSettings();
	
	// Range selectors display events listeners
	$("#micro-duration-range")[0].addEventListener('change', setTimerSliderEventListeners, false); 
	$("#micro-frequency-range")[0].addEventListener('change', setTimerSliderEventListeners, false);
	
	$("#macro-duration-range")[0].addEventListener('change', setTimerSliderEventListeners, false); 
	$("#macro-frequency-range")[0].addEventListener('change', setTimerSliderEventListeners, false); 
	
	// User preference local storage event listeners
	$("#micro-duration-range")[0].addEventListener('change', storeSettings, false);
	$("#micro-frequency-range")[0].addEventListener('change', storeSettings, false);
	
	$("#macro-duration-range")[0].addEventListener('change', storeSettings, false);
	$("#macro-frequency-range")[0].addEventListener('change', storeSettings, false);
} 

function pickStoredSettings() {
	if(microBreakSettings.getDuration())  $("#micro-duration-range")[0].value  = microBreakSettings.getDuration();
	if(microBreakSettings.getFrequency()) $("#micro-frequency-range")[0].value = microBreakSettings.getFrequency();
	
	if(macroBreakSettings.getDuration())  $("#macro-duration-range")[0].value  = macroBreakSettings.getDuration(); 
	if(macroBreakSettings.getFrequency()) $("#macro-frequency-range")[0].value = macroBreakSettings.getFrequency();
	
	updateDisplayValues();
}

function updateDisplayValues() {
	setTimerSliderEventListeners.call($("#micro-duration-range")[0]);
	setTimerSliderEventListeners.call($("#micro-frequency-range")[0]);
	
	setTimerSliderEventListeners.call($("#macro-duration-range")[0]);
	setTimerSliderEventListeners.call($("#macro-frequency-range")[0]);
}

function storeSettings() {
	breakSettings = new Array(microBreakSettings, macroBreakSettings);
	
	for(i = 0, breakSettingsObject = breakSettings[i]; i < breakSettings.length; i++, breakSettingsObject = breakSettings[i]) {
		var breakName = breakSettingsObject.getName();
	
		var breakFrequency = $("#"+breakName + "-frequency-range")[0].value; 
		var breakDuration  = $("#"+breakName + "-duration-range")[0].value; 
		var soundFile      = null;
		var soundSetting   = null;
		
		breakSettingsObject.storeSettings(breakFrequency, breakDuration, soundFile, soundSetting);
	}
	
	// Reload settings in the parent window to pick these new settings
	synchronizer.reloadSettings();
	
	// Restart the breaks with these newly found settings
	synchronizer.restartBreak();
}

//Add event handlers to range type inputs so user can see the current value as he 
//uses the slider
function setTimerSliderEventListeners() {
	$("#"+this.getAttribute("spanID"))[0].innerHTML  = convertToMinutesSeconds(this.value);
}

function convertToMinutesSeconds(value) {
	if(value < 60) return value + " seconds";
	else return Math.round((value/60)*Math.pow(10,1))/Math.pow(10,1) + " minutes";
}